package com.easysoft.commons.utils;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.BitSet;

public class URLEncode {

	private URLEncode() {
	}

	static BitSet dontNeedEncoding;

	static {
		dontNeedEncoding = new BitSet(256);
		int i;
		for (i = 'a'; i <= 'z'; i++) {
			dontNeedEncoding.set(i);
		}
		for (i = 'A'; i <= 'Z'; i++) {
			dontNeedEncoding.set(i);
		}
		for (i = '0'; i <= '9'; i++) {
			dontNeedEncoding.set(i);
		}
		dontNeedEncoding.set(' ');
		/*
		 * encoding a space to a + is done in the encode() method
		 */
		dontNeedEncoding.set('-');
		dontNeedEncoding.set('_');
		dontNeedEncoding.set('.');
		dontNeedEncoding.set('*');

		dontNeedEncoding.set('+');
		dontNeedEncoding.set('%');
	}

	/**
	 * 判断段落文本是否被urlencode过
	 * 
	 * @param str
	 * @return
	 */
	public static final boolean isURLEncoded(String str) {
		if (str == null || "".equals(str)) {
			return false;
		}
		char[] chars = str.toCharArray();
		boolean containsPercent = false;
		for (char c : chars) {
			if (Character.isWhitespace(c)) {
				return false;
			}
			if (!dontNeedEncoding.get(c)) {
				return false;
			}
			if (c == '%') {
				containsPercent = true;
			}
		}
		if (!containsPercent) {
			return false;
		}
		return true;
	}

	/**
	 * 使用UTF-8编码机制将字符串转换为 application/x-www-form-urlencoded 格式。
	 * 
	 * @param str
	 * @return
	 */
	public static final String encodeURL(String str) {
		try {
			return URLEncoder.encode(str, "utf-8");
		}
		catch (UnsupportedEncodingException e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 * 使用UTF-8编码机制对 application/x-www-form-urlencoded 字符串解码。
	 * 
	 * @param str
	 * @return
	 */
	public static final String decodeURL(String str) {
		try {
			return URLDecoder.decode(str, "utf-8");
		}
		catch (UnsupportedEncodingException e) {
			throw new RuntimeException(e);
		}
	}

}
